//
//  AppDelegate.m
//  01-UIViewHomeWork
//
//  Created by QC.L on 16/7/14.
//  Copyright © 2016年 QC.L. All rights reserved.
//

#import "AppDelegate.h"
#import "ViewController.h"

#define WIDTH self.window.frame.size.width

@interface AppDelegate ()<UITextFieldDelegate>

@end
    

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    _window.backgroundColor = [UIColor whiteColor];
    [_window makeKeyAndVisible];
    _window.rootViewController = [[ViewController alloc] init];
    
    

    /** 源图片 */
    UIImage *image = [UIImage imageNamed:@"welcome_2"];
    /** CIImage类似UIImage, 用来获取图片资源 */
    CIImage *ciImage = [[CIImage alloc] initWithImage:image];
    /** CIFilter, 高斯模糊滤镜 */
    CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"];
    /** 将图片输入到滤镜中 */
    [filter setValue:ciImage forKey:kCIInputImageKey];
    /** 用来查询滤镜中可以设置的参数以及一些相关的信息 */
    NSLog(@"%@", [filter attributes]);
    /** 设置模糊程度, 默认为10, 取值范围(0-100) */
    [filter setValue:@(0) forKey:@"inputRadius"];
    /** 将处理好的图片输出 */
    CIImage *outCiImage = [filter valueForKey:kCIOutputImageKey];
    /** CIContext */
    CIContext *context = [CIContext contextWithOptions:nil];
    /** 获取CGContext句柄, 也就是从数据流中取出图片 */
    CGImageRef outCGImage = [context createCGImage:outCiImage fromRect:[outCiImage extent]];
    /** 最终获取到图片 */
    UIImage *blurImage = [UIImage imageWithCGImage:outCGImage];
    /** 释放CGImage句柄 */
    CGImageRelease(outCGImage);
    
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [imgView setImage:[UIImage imageNamed:@"welcome_2"]];
    imgView.alpha = 0.8;
    [_window addSubview:imgView];
    
    UIImageView *photoView = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH / 2 - 65, 70, 130, 130 * 296 / 184)];
    [photoView setImage:[UIImage imageNamed:@"logo_lagou"]];
    [imgView addSubview:photoView];
    
    UIImageView *account = [[UIImageView alloc] initWithFrame:CGRectMake(15, 300, 50, 50)];
    [account setImage:[UIImage imageNamed:@"icon_username"]];
    [imgView addSubview:account];
    account.backgroundColor = [UIColor blackColor];
//    [account.layer setCornerRadius:10.0];
    [account setTag:10];
    UIBezierPath *maskPath3 = [UIBezierPath bezierPathWithRoundedRect:account.bounds byRoundingCorners:UIRectCornerTopLeft cornerRadii:CGSizeMake(5, 5)];
    CAShapeLayer *maskLayer3 = [[CAShapeLayer alloc] init];
    maskLayer3.frame = account.bounds;
    maskLayer3.path = maskPath3.CGPath;
    account.layer.mask = maskLayer3;
    
    UIImageView *password = [[UIImageView alloc] initWithFrame:CGRectMake(15, 355, 50, 50)];
    [password setImage:[UIImage imageNamed:@"icon_password"]];
    [imgView addSubview:password];
    password.backgroundColor = [UIColor blackColor];
//    [password.layer setCornerRadius:10.0];
    [password setTag:20];
    UIBezierPath *maskPath4 = [UIBezierPath bezierPathWithRoundedRect:password.bounds byRoundingCorners:UIRectCornerBottomLeft cornerRadii:CGSizeMake(5, 5)];
    CAShapeLayer *maskLayer4 = [[CAShapeLayer alloc] init];
    maskLayer4.frame = password.bounds;
    maskLayer4.path = maskPath4.CGPath;
    password.layer.mask = maskLayer4;
    
    UITextField *tf1 = [[UITextField alloc] initWithFrame:CGRectMake(65, 300, WIDTH - 80, 50)];
    tf1.backgroundColor = [UIColor blackColor];
    [_window addSubview:tf1];
//    tf1.borderStyle = UITextBorderStyleRoundedRect;
    tf1.alpha = 0.4;
    tf1.clearButtonMode = UITextFieldViewModeAlways;
    tf1.textColor = [UIColor whiteColor];
    tf1.delegate = self;
    [tf1 setTag:1];
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:tf1.bounds byRoundingCorners:UIRectCornerTopRight cornerRadii:CGSizeMake(5, 5)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = tf1.bounds;
    maskLayer.path = maskPath.CGPath;
    tf1.layer.mask = maskLayer;

    UITextField *tf2 = [[UITextField alloc] initWithFrame:CGRectMake(65, 355, WIDTH - 80 , 50)];
    tf2.backgroundColor = [UIColor blackColor];
    [_window addSubview:tf2];
//    tf2.borderStyle = UITextBorderStyleRoundedRect;
    tf2.alpha = 0.4;
    tf2.clearButtonMode = UITextFieldViewModeAlways;
    tf2.textColor = [UIColor whiteColor];
    tf2.delegate = self;
    tf2.secureTextEntry = YES;
    [tf2 setTag:2];
    UIBezierPath *maskPath1 = [UIBezierPath bezierPathWithRoundedRect:tf2.bounds byRoundingCorners:UIRectCornerBottomRight cornerRadii:CGSizeMake(5, 5)];
    CAShapeLayer *maskLayer1 = [[CAShapeLayer alloc] init];
    maskLayer1.frame = tf2.bounds;
    maskLayer1.path = maskPath1.CGPath;
    tf2.layer.mask = maskLayer1;
    
    
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeSystem];
    btn2.frame = CGRectMake( 15, 430, WIDTH - 30, 50);
    btn2.backgroundColor = [UIColor colorWithRed:0.00 green:0.68 blue:0.53 alpha:1.00];
    [btn2 setTitle:@"登录" forState:UIControlStateNormal];
    [btn2 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [btn2 addTarget:self action:@selector(logIn) forControlEvents:UIControlEventTouchUpInside];
    [_window addSubview:btn2];
    [btn2.layer setCornerRadius:10.0];
    
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeSystem];
    btn1.frame = CGRectMake(15, 500, 100, 40);
    [btn1 setTitle:@"忘记密码" forState:UIControlStateNormal];
    [btn1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btn1.titleLabel.font = [UIFont systemFontOfSize:20];
    [imgView addSubview:btn1];
    
    UIButton *log = [[UIButton alloc] initWithFrame:CGRectMake(130, 560, WIDTH - 260, 40)];
    [log setTitle:@"现在注册拉钩" forState:UIControlStateNormal];
    [log setTitleColor:[UIColor colorWithRed:0.00 green:0.68 blue:0.53 alpha:1.00] forState:UIControlStateNormal];
    log.titleLabel.font = [UIFont systemFontOfSize:22 weight:15];
    [imgView addSubview:log];
    
    UIView *line1 = [[UIView alloc] initWithFrame:CGRectMake(15, 620, WIDTH / 2 - 35, 1)];
    line1.backgroundColor = [UIColor whiteColor];
    [imgView addSubview:line1];
    
    UIView *line2 = [[UIView alloc] initWithFrame:CGRectMake(WIDTH / 2 + 20, 620, WIDTH / 2 - 35, 1)];
    line2.backgroundColor = [UIColor whiteColor];
    [imgView addSubview:line2];
    
    UILabel *or = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH / 2 - 20, 610, 40 , 20)];
    or.text = @"or";
    [imgView addSubview:or];
    or.textAlignment = NSTextAlignmentCenter;
    or.textColor = [UIColor whiteColor];
    or.font = [UIFont systemFontOfSize:25 weight:0.5];
    
    UIButton *btn3 = [[UIButton alloc] initWithFrame:CGRectMake(WIDTH / 2 - 50, 650, 100, 30)];
    [btn3 setTitle:@"随便逛逛 >" forState:UIControlStateNormal];
    [btn3 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [imgView addSubview:btn3];

    
    
    return YES;
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    if (textField.tag == 1) {
        UIImageView *i = [_window viewWithTag:10];
        [i setImage:[UIImage imageNamed:@"icon_username_select"]];
    }
    if (textField.tag == 2) {
        UIImageView *i = [_window viewWithTag:20];
        [i setImage:[UIImage imageNamed:@"icon_password_select"]];
    }
    return YES;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    if (textField.tag == 1) {
        UIImageView *i = [_window viewWithTag:10];
        [i setImage:[UIImage imageNamed:@"icon_username"]];
    }
    if (textField.tag == 2) {
        UIImageView *i = [_window viewWithTag:20];
        [i setImage:[UIImage imageNamed:@"icon_password"]];
    }
    return YES;
}

/**< 当点击键盘上的return按键. */
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    // 隐藏键盘
    [textField resignFirstResponder];
    return YES;
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    // 隐藏键盘
    [_window endEditing:YES];
    
}

- (void)logIn {
    NSLog(@"未实现");
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
